package Com.Ring.TradingMobile;

import Com.Ring.TradingMobile.Object.DanhMuc;
import Com.Ring.TradingMobile.Object.TableName;
import Com.Ring.TradingMobile.SQLite.DBManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class DanhMucActivity extends ListActivity {
	StringBuilder str = new StringBuilder();

	private DBManager dbData;
	private SQLiteDatabase writedb;
	private int PosItem;

	private static final int THEM_DANH_MUC = 1;
	private static final int XOA_DANH_MUC = 2;
	private static final int SUA_DANH_MUC = 3;
	
	private static final String[] FROM_DANHMUC = new String[] {  DanhMuc.NAME_DANH_MUC };
	private static final int[] TO_DANHMUC = new int[] { R.id.itemDM };
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateDialog(int)
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case THEM_DANH_MUC:
			LayoutInflater factory = LayoutInflater.from(this);
			final View themView = factory
					.inflate(R.layout.dialog_danhmuc, null);
			AlertDialog.Builder builder = new AlertDialog.Builder(
					DanhMucActivity.this);
			try {
				EditText txtdanhMuc = (EditText) findViewById(R.id.editTenDanhMuc);
				if(txtdanhMuc!=null){
					txtdanhMuc.setText("");
				}
				builder.setIcon(R.drawable.alert_dialog_icon);
				builder.setTitle(R.string.strTenDanhMuc);
				builder.setView(themView);
				builder.setPositiveButton(R.string.str_ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
							
									EditText txt = (EditText)themView.findViewById(R.id.editTenDanhMuc);
								if (!txt.getText().toString().trim()
										.equals("")) {
									dbData.Insert(TableName.DanhMuc, new String[]{txt.getText().toString()});
									txt.setText("");
									Refresh();
								}
							}
						});

				builder.setNegativeButton(R.string.str_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {

							}
						});
			} catch (Exception ex) {
				String cach = ex.toString();
			}
			return builder.create();

		case XOA_DANH_MUC:
			AlertDialog.Builder builderXoa = new Builder(DanhMucActivity.this);
			builderXoa.setIcon(R.drawable.alert_dialog_icon);
			builderXoa.setTitle(R.string.strCofirmXoa);
			builderXoa.setPositiveButton(R.string.str_ok, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					Object obj = getListAdapter().getItem(getSelectedItemPosition());
					dbData.Delete(TableName.DanhMuc, new String[]{obj.toString()});
					Refresh();
				}
			});
			builderXoa.setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					
				}
			});
			return builderXoa.create();
		}
		return null;
	}
	
	private void Refresh(){
		
		Cursor cursor = dbData.Select(TableName.DanhMuc, "", FROM_DANHMUC); 
		if(cursor==null) return;
		startManagingCursor(cursor);
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(DanhMucActivity.this,
				R.layout.listdanhmuc, cursor, FROM_DANHMUC, TO_DANHMUC);
		adapter.notifyDataSetChanged();
		setListAdapter(adapter);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.danhmuc);
		dbData = new DBManager(this, TableName.DanhMuc);
		Refresh();
		Button btnThemDanhMuc = (Button) findViewById(R.id.btnThemDanhMuc);
		Button btnXoaDanhMuc = (Button) findViewById(R.id.btnXoaDanhMuc);
		Button btnSuaDanhMuc = (Button) findViewById(R.id.btnSuaDanhMuc);

		btnThemDanhMuc.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				showDialog(THEM_DANH_MUC);
			}
		});
		
		btnXoaDanhMuc.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				showDialog(XOA_DANH_MUC);
			}
		});
		
		btnSuaDanhMuc.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				showDialog(SUA_DANH_MUC);
				
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView,
	 * android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
	}

}
